package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdChannel;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 谢金成
 * @date 2024/4/10 14:33
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listChannel(ChannelDto dto) {
        // 1.参数校验
        dto.checkParam();
        // 2.分页查询
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        // 2.1 频道模糊查询, 时间倒序, 状态精确

        page = page(page, Wrappers.<WmChannel>lambdaQuery()
                .like(StringUtils.isNoneBlank(dto.getName()), WmChannel::getName, dto.getName())
                .orderByDesc(WmChannel::getCreatedTime));
        // 3.封装结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        // 4.结果返回
        return pageResponseResult;
    }

    /**
     * 保存频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        // 1.参数校验
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.通过频道名称查询频道
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery()
                .eq(WmChannel::getName, adChannel.getName()));
        // 3.频道不存在
        if (channel == null) {
            // 4.保存频道
            channel = new WmChannel();
            BeanUtils.copyProperties(adChannel, channel);
            channel.setCreatedTime(new Date());
            channel.setIsDefault(true);

            save(channel);
        }

        // 5.结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private WmNewsMapper wmNewsMapper;

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        // 1.参数校验
        if (adChannel == null || adChannel.getId() == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.如果是禁用频道, 则查询文章, 看是否频道被引用
        if (!adChannel.getStatus()) {
            List<WmNews> wmNewsList = wmNewsMapper.selectList(Wrappers.<WmNews>lambdaQuery()
                    .eq(WmNews::getChannelId, adChannel.getId()));
            if (wmNewsList != null && wmNewsList.size() > 0) {
                // 存在引用
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道被引用, 无法禁用");
            }
        }

        // 3.修改数据
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(adChannel, channel);
        channel.setCreatedTime(new Date());
        boolean result = updateById(channel);

        if (!result) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "频道修改失败");
        }

        // 4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        // 1.参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.查询频道
        WmChannel channel = getById(id);

        // 不存在
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 3.判断是不是禁用状态
        if (channel.getStatus()) {
            // 不是返回
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "启用的频道无法删除");
        }

        // 是禁用, 删除
        boolean result = removeById(id);

        // 返回结果
        if (!result) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除频道失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
